package com.ukao.pad.rfid;

import java.io.FileDescriptor;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;

import static android.R.attr.src;

/**
 * Created by herego on 2017/5/23.
 * 串口助手
 */

public class SerialPortHelper {
    static {
        System.loadLibrary("com_ukao_pad_rfid_SerialPortHelper");
    }
    private FileDescriptor mFileDescriptor = null;
    private FileInputStream mFileInputStream = null;
    private FileOutputStream mFileOutputStream = null;

    public SerialPortHelper(){

    }

    public boolean initPort(String devName,int baudRate){
        if((mFileDescriptor=_initPort(devName, baudRate)) != null) {
            mFileInputStream = new FileInputStream(mFileDescriptor);
            mFileOutputStream = new FileOutputStream(mFileDescriptor);
            return true;
        }
        return false;


    }

    public void closePort(){
        if(mFileDescriptor != null) {
            _closePort(this);
            mFileDescriptor = null;
        }
    }

    // Getters and setters
    public InputStream getInputStream() {
        return mFileInputStream;
    }

    public OutputStream getOutputStream() {
        return mFileOutputStream;
    }

    public static native FileDescriptor _initPort(String devName, int baudRate);
    public static native int _closePort(SerialPortHelper x);
}
